# -*- coding: utf-8 -*-
import os
import random
from urllib.request import urlretrieve


class GeneratorUtils:

    def __init__(self) -> None:
        super().__init__()
        self.__meta_letter = 'abcdefghijklmnopqrstuvwxyz'

    def gen_mobile_sell_data(self, durs: int, *brands: list) -> {}:
        '''
        生成手机销售数据
        :param durs: 周期数
        :param brands: 手机品牌列表
        :return:
        '''
        ret = {}
        for brand in brands:
            tmp = [round(random.uniform(10000, 100000), 3) for _ in range(durs)]
            ret[brand] = tmp

        return ret

    def gen_random_str(self, length: int) -> str:
        '''
        生成随机字符串
        :param length: 字符串长度
        :return: 字符串
        '''
        str = ''.join(random.sample(self.__meta_letter, length))
        return str

    def download_img(self, **kwargs) -> str:
        url = kwargs['img_url']
        img_name = kwargs['img_name']
        img_name = img_name if img_name else 'undefine/' + self.gen_random_str(4) + 'jpg'
        img_path = '/data/code/scrapy/demoi/mansite/img/{}'.format(img_name)
        os.makedirs(os.path.abspath(os.path.join(img_path, '../')), exist_ok=True)
        urlretrieve(url, img_path)
        return img_path
